require([ "require-config" ], function() {
    require([ 'jquery', 'validate' ], function() {
        $('#roleSelect').on('change', function() {
            var item = $(this).children('option:selected'), itemValue = item.data("value");
            if (itemValue != 0) {
                var itemLabel = item.data("label");
                $('#role' + itemValue).remove();
                $('#roles').append('<div id="role' + itemValue + '"><i class="fa fa-minus-circle"></i> ' + itemLabel + '<input type="hidden" name="roleIds" value="' + itemValue + '" /></div>');
            }
        });
        $('#roles').delegate('.fa-minus-circle', 'click', function() {
            $(this).parent().remove();
        });

        $('form').validate({
            rules : {
            	memberAccount: {
                    required: true
                },
                memberPassword: {
                    required: true
                },
                memberName: {
                    required: true
                },
                memberPhone: {
                    required: true
                },
                memberEmail: {
                    required: true
                },
                memberLeader: {
                    required: true
                }
            },
            messages : {
            	memberAccount: {
                    required: '用户名不能为空'
                },
                memberPassword: {
            		required: '密码不能为空'
            	},
            	memberName: {
            		required: '密码不能为空'
            	},
            	memberPhone: {
            		required: '密码不能为空'
            	},
            	memberEmail: {
            		required: '密码不能为空'
            	},
            	memberLeader: {
            		required: '密码不能为空'
            	}
            },
            submitHandler : function(form) {
                $.ajax({
                    url: 'play',
                    type: 'POST',
                    data: {
                        'memberAccount': $('#memberAccount').val(),
                        'invest': $('#invest').val(),
                        'bonus': $('#bonus').val(),
                        'playInfo': $('#playInfo').val()
                    },
                    dataType: 'json',
                    beforeSend: function() {
                        $("#frm_submit").attr("disabled", true);
                    },
                    success: function(response) {
                        if (true == response.success) {
                        	alert(response.message);
                            window.location.href = 'list';
                        } else {
                            alert(response.message);
                        }
                        $("#frm_submit").attr("disabled", false);
                    },
                    error: function() {
                        $("#frm_submit").attr("disabled", false);
                    }
                });
            }
        });
    });
});